//
//  PersonDetailViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/25.
//

#import "PersonDetailViewController.h"
#import "CommonUtil.h"
#import "PersonHeaderView.h"
#import "LeaingMessageTableViewCell.h"
#import "MessageModel.h"
#import "ReplayDetailViewController.h"
#import "ReplayViewController.h"

@interface PersonDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) PersonHeaderView * headerView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) UIButton *toolBar;
@end

@implementation PersonDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.toolBar];
    
    [self testData];
    
}

// 假数据
- (void)testData{
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        MessageModel *model = [[MessageModel alloc] init];
        model.content = @"根据国家卫健委24日下午公布的最新数据"; // 测试单行文字
        if (i < 10) {
            model.imageCount = i; //测试图片展示
        }
        if (i == 1 || i == 5) {
            model.content = @"环球时报记者 倪浩】根据国家卫健委24日下午公布的最新数据，截至5月23日，全国累计报告接种新冠病毒疫苗51085.8万剂次。在安徽和辽宁新一轮局部疫情之后，中国疫苗接种明显提速。5月19日之前的8天时间"; //测试多行文字
        }
        
        if (i == 0 || i == 6) {
            [model addReayTest];// 测试回复内容最多显示3条
        }

        [self.models addObject:model];
    }
    [self.tableView reloadData];
}


- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
 

}


- (void)viewWillDisappear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = false;

}


- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)toolBarBtnClick:(UIButton *)sender{
    [self presentViewController:[ReplayViewController new] animated:YES completion:nil];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaingMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.superVC = self;
    if (self.models.count > indexPath.row) {
        cell.model = self.models[indexPath.row];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.navigationController pushViewController:[ReplayDetailViewController new] animated:YES];


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.models.count > indexPath.row) {
        MessageModel *model = self.models[indexPath.row];
        return model.cellHeight;
    }
    return AD(100);
}




-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_tableView registerClass:[LeaingMessageTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kTabBarHeight, 0);
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
    }
    return _tableView;
}

-(PersonHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[PersonHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(260))];
        [_headerView.backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}

-(UIButton *)toolBar{
    if (!_toolBar) {
        _toolBar = [UIButton buttonWithType:UIButtonTypeCustom];
        [_toolBar addTarget:self action:@selector(toolBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _toolBar.backgroundColor = RGB(249.0, 249.0, 249.0);
        _toolBar.frame = CGRectMake(0, SCREEN_HEIGHT-AD(80), SCREEN_WIDTH, AD(80));
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(AD(15), AD(15), SCREEN_WIDTH-AD(80), AD(40))];
        v.backgroundColor = [UIColor whiteColor];
        v.layer.cornerRadius = AD(5);
        v.layer.borderColor = RGB(240.0, 240.0, 240.0).CGColor;
        v.layer.borderWidth = AD(1);
        v.userInteractionEnabled = false;
        [_toolBar addSubview:v];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(v.right + AD(10), v.top, AD(50), AD(40))];
        label.text = @"留言";
        label.textColor = RGB(255.0, 77.0, 110.0);
        label.font = [UIFont systemFontOfSize:AD(17)];
        [_toolBar addSubview:label];
        
    }
    return _toolBar;
}

@end
